<template>
	<slot></slot>
</template>

<script lang="ts" setup>
import { onBeforeMount, provide, reactive } from "vue";
import { promiseInjectionKey } from "../help";
import type { PromiseResponse } from "../help";

const props = withDefaults(
	defineProps<{
		promise: () => void;
	}>(),
	{},
);

const result = reactive<PromiseResponse>({
	result: null,
	error: null,
});

provide(promiseInjectionKey, result);

const http = async () => {
	if (!props.promise) {
		result.error = "promise can not find";
	} else {
		try {
			result.result = await props.promise();
		} catch (error) {
			result.error = error;
		}
	}
};

onBeforeMount(() => {
	http();
});

defineExpose({
	http,
});
</script>
